package com.zhou.common.operator.log.storage.api;


import com.zhou.common.operator.log.core.OperateLogProperty;
import com.zhou.common.operator.log.core.ao.OperateLogInfoGetPageListAO;
import com.zhou.common.operator.log.core.model.OperateLogInfo;
import com.zhou.common.operator.log.core.service.impl.AbstractStorageService;
import com.zhou.common.operator.log.storage.api.feign.OperateLogFeignClient;
import com.zhou.common.pub.model.CommonPage;
import com.zhou.common.pub.model.Response;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;
import java.util.List;

public class RestApiStorgeService extends AbstractStorageService {

    @Autowired
    private OperateLogFeignClient operateLogFeignClient;

    public RestApiStorgeService(OperateLogProperty property) {
        super(property);
    }

    @Override
    public CommonPage<OperateLogInfo> getPageList(OperateLogInfoGetPageListAO operateLogInfoGetPageListAO) {
        Response<CommonPage<OperateLogInfo>> response = this.operateLogFeignClient.list(operateLogInfoGetPageListAO);
        return response != null && response.getData() != null ? (CommonPage) response.getData() : null;
    }

    @Override
    protected void saveBatch(List<OperateLogInfo> logs) {
        List<OperateLogRecord> recordList = new ArrayList();
        logs.stream().forEach((o) -> {
            OperateLogRecord operateLogRecord = new OperateLogRecord();
            operateLogRecord.convertTo(o);
            recordList.add(operateLogRecord);
        });
        this.operateLogFeignClient.append(recordList);
    }
}

